package com.qf.listener.simple;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @RabbitListener: 监听器 器 注解，用来监听对列中是否有消息
 * @RabbitHandler； 执行处理消息的方法
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Component
//@RabbitListener(queues = {"simple_queue"})
public class SimpleMessageListener_001 {

    @Autowired
    StringRedisTemplate redisTemplate;

    @RabbitHandler
    public void consum(String msg, Channel channel, Message message) throws IOException {
        try {
            System.out.println("消息队列中的消息:" + msg);
//            int i = 1/0;
            //执行成功
            /**
             * 参数1： 队列消息的唯一的标识
             * 参数2：是否批量处理
             */
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            // 出现异常
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);

        }


    }
}
